package com.clss.emergencycall.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/2/28 0028.
 */

public class DateUtil {

    public static String time2Date(String time) {
        if (time == null || time.equals("")) {
            return "";
        }
        try {
            Date d = new Date(Long.valueOf(time));
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sf.format(d);
        } catch (NumberFormatException e) {
            return time;
        }
    }

    public static String time2Date(Long time) {
        if (time == 0) {
            return "";
        }
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /*字符窜转换成时间戳*/
    public static Long stringToDate(String str) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long timeStemp = date.getTime();
        return timeStemp;
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    /**
     * 时间戳转换 到秒
     *
     * @param time
     * @return
     */
    public static String getDateToString2(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    public static String getDateStringToString(String time) {
        if (time == null || time.equals(""))
            return "";
        try {
            Date d = new Date(Long.valueOf(time));
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sf.format(d);
        } catch (NumberFormatException e) {
            return time;
        }

    }

    public static String getStringToData(long date) {
        Date d = new Date(date);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    public static String getDateToString3(long date) {
        Date d = new Date(date);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        return sf.format(d);
    }

    public static String getDateStringToString3(String date) {
        Date d = new Date(Long.valueOf(date));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        return sf.format(d);
    }

    public static String getNowTime() {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("yyyy年MM月dd日 HH:mm");
//        java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("HH:mm");
        return formater.format(new Date());
    }
}
